package com.wan.fragment


import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.gson.Gson

import com.wan.R
import com.wan.adapter.HomeRvAdapter
import com.wan.base.BaseBean
import com.wan.base.BaseLazyFragment
import com.wan.entity.HomeArtEntity
import com.wan.entity.HomeArtItemEntity
import com.wan.entity.SystemTabEntity
import com.wan.entity.SystemTabSonEntity
import com.wan.uitls.APIServices
import com.wan.uitls.CommonUtils
import com.wan.uitls.ItemRightViewListener
import kotlinx.android.synthetic.main.fragment_system_son_base.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory


/**
 * todo
 * 这里有一个问题就是 关于如果用Rv的话虽然解决掉了 内存消耗过大的问题
 * 但是对于下拉刷新之类的 ,对于原来的状态保存,也是不行的,
 * 比如我滑动到 原来的第十一个位置,涉及到第二个page ,怎么保存这是个问题???
 * 待修正,考虑改成fragment形式
 *
 */
class SystemSonBaseFragment : BaseLazyFragment(), ItemRightViewListener {
    override fun onRightViewClickListener(position: Int) {
        CommonUtils.toastWord(context!!, "right pic view click")
    }


    private val threadName = this.javaClass.name

    companion object {
        const val DATA = "data"

        fun newInstance(bundle: Bundle): SystemSonBaseFragment {
            var fragment = SystemSonBaseFragment()
            fragment.arguments = bundle
            return fragment
        }
    }

    private var titleList = ArrayList<SystemTabSonEntity>()
    var dataList = ArrayList<HomeArtItemEntity>()


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_system_son_base, container, false)
    }


    override fun initData() {
        Log.e("110", "执行了initData")
        var data = arguments!!.getString(DATA)
        var gson = Gson()
        var systemTabEntity = gson.fromJson<SystemTabEntity>(data, SystemTabEntity::class.java)
        titleList.addAll(systemTabEntity.children)
        Log.e(threadName, "子碎片获取到的数据 == > ${systemTabEntity.toString()}")
        systemTabEntity.children.forEach {
            system_son_tab.addTab(system_son_tab.newTab().setText(it.name))

        }

        system_son_rv.layoutManager = LinearLayoutManager(context)

        var homeRvAdapter = HomeRvAdapter(context!!, dataList, R.layout.fragment_home_item_layout)
        system_son_rv.adapter = homeRvAdapter

        system_son_tab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                var position = tab!!.position
                handleAPIData(position)
            }
        })
        handleAPIData(0)

    }

    private fun handleAPIData(position: Int) {
        var retrofit = Retrofit.Builder()
                .baseUrl(APIServices.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
        var apiServices = retrofit.create(APIServices::class.java)
        apiServices.getSystemArtItemList(0, titleList[position].id).enqueue(object : Callback<BaseBean<HomeArtEntity>> {
            override fun onFailure(call: Call<BaseBean<HomeArtEntity>>, t: Throwable) {
                Log.e(threadName, "失败了")
            }

            override fun onResponse(call: Call<BaseBean<HomeArtEntity>>, response: Response<BaseBean<HomeArtEntity>>) {
                Log.e(threadName, "成功了")
                dataList.clear()
                dataList.addAll(response.body()!!.data.datas)
                system_son_rv.adapter.notifyDataSetChanged()
            }
        })
    }


}
